package model.graphic.objects;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import model.graphic.locations.FlexibleRelativeLocation;
import model.graphic.locations.Location;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Jelle, modified by Ben
 */
public class GInput extends AbstractHandle {
    private Location p1, p2, p3, p4,p5;
    public static final int EXTRA_WIDTH = 5;

    public GInput() {
    }

    private Polygon getTriangle() {
        final int WIDTH = GOutput.ARROW_DELTA;
        p1 = new FlexibleRelativeLocation(((RectangleAttachment) base).getUpperLeft(), -WIDTH -EXTRA_WIDTH, 0);
        p2 = new FlexibleRelativeLocation(((RectangleAttachment) base).getUpperLeft(), 0, 0);
        p3 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), 0, 0);
        p4 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), -WIDTH -EXTRA_WIDTH+1, 0);
        p5 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), -EXTRA_WIDTH, -WIDTH+1);
        int[] xs = {p1.getX(), p2.getX(), p3.getX(),p4.getX(),p5.getX()};
        int[] ys = {p1.getY(), p2.getY(), p3.getY(),p4.getY(),p5.getY()};
        Polygon triangle = new Polygon(xs, ys, xs.length);
        return triangle;
    }

    private Polygon getBigTriangle() {
        final int WIDTH = GOutput.ARROW_DELTA;
        p1 = new FlexibleRelativeLocation(((RectangleAttachment) base).getUpperLeft(), -WIDTH -EXTRA_WIDTH -4, -2);
        p2 = new FlexibleRelativeLocation(((RectangleAttachment) base).getUpperLeft(), 0, -2);
        p3 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), 0, 2);
        p4 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), -WIDTH -EXTRA_WIDTH-3, 2);
        p5 = new FlexibleRelativeLocation(((RectangleAttachment) base).getLowerLeft(), -EXTRA_WIDTH -2, -WIDTH +1);
        int[] xs = {p1.getX(), p2.getX(), p3.getX(),p4.getX(),p5.getX()};
        int[] ys = {p1.getY(), p2.getY(), p3.getY(),p4.getY(),p5.getY()};
        Polygon triangle = new Polygon(xs, ys, xs.length);
        return triangle;
    }

    public GInput(Attachment baseHandle) {
        super(baseHandle, new FlexibleRelativeLocation(((RectangleAttachment) baseHandle).getUpperRight(), 0, 0));
    }

    public void move(Attachment base){
        adjustLocation(base, new FlexibleRelativeLocation(((RectangleAttachment) base).getUpperRight(), 0, 0));
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        this.setHighlighted(true);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        this.setHighlighted(false);
        GraphicView gv = (GraphicView) this.getParent();
        gv.setInputFound(null);
        gv.repaint();
    }

    @Override
    public void paint(Graphics2D g) {
        if (this.isVisible() && (base.isVisible() || ((RectangleAttachment)base).isCollapsed())) {
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();

            g.setColor(backColor);
            g.fillPolygon(getTriangle());

            if (selected) {
                g.setColor(highlightColor);
            } else {
                g.setColor(baseColor);
            }
            g.drawPolygon(getTriangle());

            g.draw(new Ellipse2D.Double(p5.getX(), p5.getY(), 0, 0));//bugfix in java

            if (highlighted) {
                //g.setStroke(new BasicStroke(2.0f));
                g.setColor(highlightColor);
                g.drawPolygon(getTriangle());
                g.drawPolygon(getBigTriangle());
            }
            g.setStroke(oldStroke);
            g.setColor(oldColor);
        }
    }

    @Override
    public boolean contains(int x, int y) {
        return getTriangle().contains(x+getX(), y+getY());
    }

    @Override
    public void setName(String name) {
        super.setName(name);
        this.setToolTipText(LabelReader.toHTML(this.name + "\n" + this.type));
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        this.setHighlighted(true);
        GraphicView gv = (GraphicView) this.getParent();
        gv.setInputFound(this);
        gv.repaint();
    }

}
